package cn.zhou.jin.service.impl;


import cn.zhou.basic.exception.BusinessException;
import cn.zhou.basic.service.impl.BaseServiceImpl;
import cn.zhou.jin.domain.Employee;
import cn.zhou.jin.domain.Shop;
import cn.zhou.jin.domain.ShopAuditLog;
import cn.zhou.jin.mapper.EmployeeMapper;
import cn.zhou.jin.mapper.ShopAuditLogMapper;
import cn.zhou.jin.mapper.ShopMapper;
import cn.zhou.jin.service.IShopService;
import cn.zhou.jin.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author me
 * @since 2022-06-12
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;
    @Override
    public void settlement(Shop shop) {
        Employee admin = shop.getAdmin();
//        *  一：参数空校验  即使前端前端写了校验，我们也需要写自己的后台校验逻辑
        if(StringUtils.isEmpty(shop.getName())
                || StringUtils.isEmpty(shop.getAddress())
                || StringUtils.isEmpty(admin.getUsername())
        ){
            throw new BusinessException("参数不能为空！！！");
        }
//        *    。。不能为空
//        *  二：合法校验   百度AI智能审核接口做
        /*if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("骚年，不要搞事哦，小心进局子！！！");
        }*/
//        *  三：判断是否曾经已经注册过
        Shop shopTmp = shopMapper.loadByName(shop.getName());
//        *    1. 店铺不能被注册
        if(shopTmp != null){
            throw new BusinessException("店铺已经被注册，请直接登录，如果忘记密码，请走找回密码流程！！！");
        }
//        *    2.人是否被注册
        Employee empTmp = employeeMapper.loadByUsername(admin.getUsername());
        if(empTmp != null && empTmp.getShop_id() != null){
//        *      2.2 注册了并且已经绑定了店铺
            throw new BusinessException("该用户已经存在运营店铺，请成功新换人！！");
        }
//        *      2.1 注册了但是没有绑定店铺  === 直接绑定
        if(empTmp == null){
            employeeMapper.add(admin);//要返回自增ID
            empTmp = admin;
        }
        //  empTmp 一定有值
//        *  四：保存数据
//        *    shop
        shop.setAdminId(empTmp.getId());
        shopMapper.add(shop);//返回自增ID
//        *    employee  shopId
//        *    反过来修改shop中的admin_id
        empTmp.setShop_id(shop.getId());
        employeeMapper.update(empTmp);

    }

    @Override
    public void pass(ShopAuditLog log) {
        Shop shop=shopMapper.loadById(log.getShopId());
        shop.setState(1);

        shopMapper.update(shop);
        log.setAuditId(368l);
        shopAuditLogMapper.add(log);
        //3.发送激活邮件
        Employee employee = employeeMapper.loadByShopId(log.getShopId());
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("627509481@qq.com");
        message.setTo(employee.getEmail());
        message.setSubject("孤儿乐园-店铺入驻激活请求");
        message.setText("亲，您的店铺信息已经通过审核，请尽快完成激活并使用：" +
                "激活链接：http://localhost:8080/shop/activation/"+log.getShopId());
        javaMailSender.send(message);
    }

    @Override
    public void reject(ShopAuditLog log) {
        //1.修改店铺状态为---驳回
        Shop shop = new Shop();
        shop.setState(-1);
        shopMapper.update(shop);
        //2.记录审核未通过日志
        log.setAuditId(334L);
        shopAuditLogMapper.add(log);
        //3.发送修改链接
        Employee employee = employeeMapper.loadByShopId(log.getShopId());
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("627509481@qq.com");
        message.setTo(employee.getEmail());
        message.setSubject("宠物乐园-店铺入驻驳回任务");
        message.setText("亲，您的店铺信息有误，审核不通过，请尽快修改并重新提交审核！！修改链接：" +
                "http://localhost:8081/#/register2?shopId="+log.getShopId());
        javaMailSender.send(message);
    }

    @Override
    public void adds(List<Shop> shops) {


           shopMapper.adds(shops);

    }

    @Override
    public List<ShopVo> queryEcharts() {
        return shopMapper.queryEcharts();
    }
}
